package pack

import (
	"github.com/cloudwego/hertz-examples/bizdemo/hertz_gorm/biz/hertz_gen/user_manager"
	"github.com/cloudwego/hertz-examples/bizdemo/hertz_gorm/biz/model"
	"github.com/cloudwego/hertz/pkg/common/json"
	"time"
)

// FileTasks Convert model.FileTask list to user_gorm.User list
func FileTasks(models []*model.FileTask) []*user_manager.FileTask {
	tasks := make([]*user_manager.FileTask, 0, len(models))
	for _, m := range models {
		if t := FileTask(m); t != nil {
			tasks = append(tasks, t)
		}
	}
	return tasks
}

var TaskTypeCname = map[user_manager.FileTaskType]string{
	user_manager.FileTaskType_PDF_WORD:  "PDF转Word",
	user_manager.FileTaskType_PDF_EXCEL: "PDF转Excel",
	user_manager.FileTaskType_WORD_PDF:  "Word转PDF",
	user_manager.FileTaskType_EXCEL_PDF: "Excel转PDF",
}

// FileTask Convert model.FileTask to user_gorm.User
func FileTask(model *model.FileTask) *user_manager.FileTask {
	var (
		sourceFiles []*user_manager.File
		targetFiles []*user_manager.File
	)
	sourceFilesBytes, _ := model.SourceFiles.MarshalJSON()
	_ = json.Unmarshal(sourceFilesBytes, &sourceFiles)
	targetFilesBytes, _ := model.TargetFiles.MarshalJSON()
	_ = json.Unmarshal(targetFilesBytes, &targetFiles)

	return &user_manager.FileTask{
		TaskID:     model.TaskID,
		TaskName:   model.TaskName,
		TaskType:   user_manager.FileTaskType(model.TaskType),
		TaskStatus: user_manager.FileTaskStatus(model.TaskStatus),
		SourceFile: sourceFiles,
		TargetFile: targetFiles,
		CreateTime: model.CreatedAt.Format(time.DateTime),
		UpdateTime: model.UpdatedAt.Format(time.DateTime),
	}
}

func FileTask2Model(fileTask *user_manager.FileTask) *model.FileTask {
	modelTask := &model.FileTask{
		TaskID:     fileTask.TaskID,
		TaskName:   fileTask.TaskName,
		TaskType:   int8(fileTask.TaskType),
		TaskStatus: int8(fileTask.TaskStatus),
	}
	modelTask.SourceFiles, _ = json.Marshal(fileTask.SourceFile)
	modelTask.TargetFiles, _ = json.Marshal(fileTask.TargetFile)
	return modelTask
}
